﻿using System.Text;
using fastJSON;
using System.IO;

namespace Development.Network.Msg
{
    public class JsonMsg : MsgBase
    {
        public override byte[] Pack()
        {
            string jstr = JSON.ToJSON(this);
            byte[] jBuffer = Encoding.UTF8.GetBytes(jstr);
            int totalLength = jBuffer.Length + 16;
            byte[] msgBuffer = new byte[totalLength];
            using(MemoryStream stream = new MemoryStream(msgBuffer))
            { 
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write(totalLength);
                writer.Write(_MsgId);
                writer.Write(_UserId);
                writer.Write(_Status);
                writer.Write(jBuffer);
            }
            _Lenth = totalLength;
            return msgBuffer;
        }
        public override void Unpack(MsgPacket packet)
        {
            base.Unpack(packet);
            string json = Encoding.UTF8.GetString(packet.Data);
            JSON.FillObject(this, json);
        }
       
    }
}
